<?php

class UnionController extends Controller {

    public function filters() {
        return array(
            'accessControl',
            'postOnly + delete', //
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view', 'list', 'getCategory', 'getBrands', 'getModels'),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex() {
        $this->render('index');
    }

    public function actionList($id) {
        $model = Page::model()->findByPk($id);
        $sql = 'SELECT "pageId", "pageTitle", "pageParent", "pageImages" FROM mb_pages WHERE "parentModel" = \'' . $id . '\'';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        $this->render('list', array('mainModel' => $model, 'models' => $models));
    }

    public function actionView() {

        if (empty($_POST))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');

        if (isset($_POST['models']) && !empty($_POST['models'])) {
            $sql = 'SELECT "pageId", "pageTitle", "pageUrl", "pageImages" FROM mb_pages WHERE "pageId" IN(' . implode(',', $_POST['models']) . ')';
            $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
            $this->render('view', array('models' => $models));
        }

        if (isset($_POST['models_marge'])) {
            $mainModel = Page::model()->findByPk((int) $_POST['mainModel']);
            $mainModelId = $mainModel->pageId;
            if (isset($_POST['new'])) {
                $mainModel->pageId = -1;
                $mainModel->save(false);
                $model = $mainModel;
                $model->pageId = null;
                $model->pageTitle = $_POST['title'];
                $model->pageUrl = $_POST['url'];
                $model->isNewRecord = true;
                if ($model->save(false)) {
                    $oldId = $model->pageId;
                    $model2 = Page::model()->findByPk((int) $model->pageId);
                    $model2->pageId = $mainModelId;
                    $model2->save(false);
                    $mainModel = Page::model()->findByPk(-1);
                    $mainModel->pageId = $oldId;
                    $mainModel->parentModel = $mainModelId;
                    $mainModel->save(false);
                }
            } else
                unset($_POST['models_marge'][$_POST['mainModel']]);
            foreach ($_POST['models_marge'] as $key => $value) {
                if ($value != $mainModelId) {
                    $sql = 'UPDATE "mb_pages" SET "parentModel"=\'' . (int) $_POST['mainModel'] . '\' WHERE ("pageId"=\'' . (int) $value . '\')';
                    Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
                }
            }
            $this->redirect(array('list', 'id' => $mainModelId));
        }
    }

    public function actionGetCategory() {
        $sql = 'SELECT "pageId", "pageTitle" FROM mb_pages WHERE "pageType" = \'category\' ORDER BY "pageSort"';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        $select = '';
        $select .='<option value="0">-- --</option>';
        foreach ($models as $model)
            $select .='<option value=\'' . $model['pageId'] . '\'>' . $model['pageTitle'] . '</option>';
        echo $select;
    }

    public function actionGetBrands($id) {
        $sql = 'SELECT "pageId", "pageTitle" FROM mb_pages WHERE "pageType" = \'brand\' AND "pageParent" = ' . (int) $id . '  ORDER BY "pageSort"';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();

        $select = 'Бренд <select name="brand">';
        $select .='<option value="0">-- --</option>';
        foreach ($models as $model)
            $select .='<option value=\'' . $model['pageId'] . '\'>' . $model['pageTitle'] . '</option>';
        echo $select . '</select>';
    }

    public function actionGetModels($id) {
        $sql = 'SELECT "pageId", "pageTitle", "pageUrl" FROM mb_pages WHERE "pageType" = \'model\' AND "pageParent" = ' . (int) $id . '  ORDER BY "pageTitle"';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($models as $model) {
            echo '<tr>'
            . '<td>'
            . '<input type="checkbox" name="models[]" value="' . $model['pageId'] . '">'
            . '</td>'
            . '<td>' . $model['pageId'] . '</td>'
            . '<td>' . $model['pageTitle'] . '</td>'
            . '<td>' . CHtml::link($model['pageTitle'], array('list', 'id' => $model['pageId']), array('target' => '_blank')) . '</td>'
            . '</tr>';
        }
    }

}
